Tycho provides a set of classes for asking the user questions. These can be yes-no questions, questions that require a typed response, or questions that require a file name response.
The YesNoQuery class poses a question and offers two buttons,
labeled "Yes" and "No". For example:
::tycho::YesNoQuery .y -text {Are you awake?} \
-yescommand {inform {You said yes!}} \
-nocommand {inform {Then how did you click on the No button?}}
.y centerOnScreen
if [::tycho::DialogWindow::newModal YesNoQuery .z -text {Are you awake?}] {
inform {You said yes!}
} {
inform {Then how did you click on the No button?}
}
There is a bit of a technicality with using this capability.
Suppose you wish to create a dialog box that returns the strings
"yes" or "no" instead of 0 or 1. The -yescommand and -nocommand
options specify Tcl scripts, and it is difficult to get a Tcl
script to return a value. You could use the "set" command as
follows:
set x [
::tycho::DialogWindow::newModal YesNoQuery .z -text {Are you awake?} \
-yescommand {set y {yes}} \
-nocommand {set y {no}}
]
inform "Your answer was $x"
set x [
DialogWindow::newModal YesNoQuery .z -text {Are you awake?} \
-yescommand {DialogWindow::answer {yes}} \
-nocommand {DialogWindow::answer {no}}
]
::tycho::inform "Your answer was $x"
The YesNoCancel class is like the YesNoQuery class, but there is one
more button labeled "Cancel" and one more option
-cancelcommand.
To see an example, execute this:
set x [::tycho::DialogWindow::newModal YesNoCancel .z -text {Are you awake?}]
::tycho::inform "The value returned was $x"
The EntryQuery class opens a dialog box with one or more text entry widgets.
It is used by the "queryinfo"
procedure described above.
You can also create instances of it directly.
It creates a dialog box with one or more entry widgets, an OK button,
and a Cancel button. The actions taken in response to these buttons
are given by the -okcommand and
-cancelcommand options.
By default, the -okcommand just returns the value entered by the user.
The -cancelcommand does nothing. After either command is executed,
the widget is destroyed. The
-queries option specifies the initial
list of entry boxes that will appear in the window.
The value of this option should be a list of three or four-element lists.
The first element of each list is an arbitrary tag used to identify
the entry box. The second element is the label to put next to the
entry box. The third element is the default string to put into
entry box. The optional fourth element specifies the number of text
lines that should be allowed in the entry box. This defaults to one.
For example,
::tycho::EntryQuery .z \
-queries {{first {First entry:} {Default string}} \
{second {Second entry:} {Another default}}} \
-entrywidth 60 \
-okcommand {inform "Result of a get: [.z get]"} \
-cancelcommand {inform "You canceled"}
.z centerOnScreen
Alternatively, entry boxes can be added after the EntryQuery object
is created using the
addQuery method.
This method takes three arguments,
a tag, a label, and a default string. These serve the same function
as the elements passed in a -queries option. The removeQuery method
takes a tag as an argument and simply removes the corresponding query.
Click again on the above example to create the EntryQuery, if you dismissed
it. Then try this example:
The class is derived from DialogWindow,
so it has the
options -text, -bitmap, and -title, which control the text
of the query, a bitmap left of the query, and a window manager title.
For example,
You also have access to the "new" and "newModal" methods of the base class.
For example,
A rather more elaborate dialog box is a file browser.
Consider the example:
As usual, you can get a modal file browser using newModal.
.z addQuery third {Third entry:} {Yet another default} 4
.z removeQuery first
::tycho::inform [.z get second]
.z clear
.z insert third {An inserted string. }
::tycho::EntryQuery .aa \
-bitmap questhead \
-text {Enter modified values, then click OK} \
-queries {{first {First entry:} {Default string}} \
{second {Second entry:} {Another default}}} \
-entrywidth 20 \
-title "EntryQuery Widget" \
-okcommand {inform "Result of a get: [.aa get]"} \
-cancelcommand {inform "You canceled"}
.aa centerOnScreen
::tycho::inform [::tycho::DialogWindow::newModal EntryQuery .z \
-queries {{onlytag {Enter a value} {default value}}}]
::tycho::FileBrowser .f -command "inform"
wm deiconify .f
::tycho::inform [::tycho::DialogWindow::newModal FileBrowser .w]
Copyright © 1996, The Regents of the University of California.
All rights reserved.
Last updated: 96/04/09,
comments to: